package com.controller;

import com.entity.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
public class SessionDemoController {

    @Autowired
    private ServerProperties serverProperties;

    @RequestMapping("/w")
    public String WirtSession(HttpSession session){
        System.out.println("serverProperties.getPort() = " + serverProperties.getPort());
        System.out.println("session.getId() = " + session.getId());
        session.setAttribute("w",new Date());
        return "ok";
    }

    @RequestMapping("/r")
    public String ReadSession(HttpSession session){
        System.out.println("serverProperties.getPort() = " + serverProperties.getPort());
        Object attribute = session.getAttribute("w");
        return attribute==null?"on session":attribute.toString();
    }

    @RequestMapping("/w2")
    public String WirtSession2(HttpSession session){
        System.out.println("serverProperties.getPort() = " + serverProperties.getPort());
        session.setAttribute("a",new Emp(1,"aa"));

        return "ok";
    }

    @RequestMapping("/r2")
    public String ReadSession2(HttpSession session){
        System.out.println("serverProperties.getPort() = " + serverProperties.getPort());
        Object attribute = session.getAttribute("a");
        return attribute==null?"on session":attribute.toString();
    }
}
